/**
 * @author yxw
 * date : 2013年10月15日 下午7:05:11 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.view.MyProgressDialog;
import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.domain.MyResultList;
import com.tzinfo.tzt.domain.NewhouseNewsDetail;
import com.tzinfo.tzt.util.HttpTools;
import com.tzinfo.tzt.util.JsonTools;

public class MoreNewsActivity extends BaseActivity implements OnClickListener,OnItemClickListener{
	
	private ListView newsListView;
	private List<Map<String, Object>> newsData;
	private MyResultList newHouseNewsListResult;
	private String sHouseId="";
	private SimpleAdapter newsSimpleAdapter;
	
	private Button backBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_more_news);
		Intent thisIntent=this.getIntent();
		sHouseId=thisIntent.getStringExtra("houseId");
		init();
	}
	private void init(){
		backBtn=(Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);
		mProgressBar = (ProgressBar)findViewById(R.id.progressbar_loding);
		// 加载进度条
		showProgressBar();
		newsListView=(ListView) this.findViewById(R.id.listview_news);
		newsListView.setOnItemClickListener(this);
		new Thread(getNewHouseNewsListRun).start();
	}
	
	private void updateUI(){
		newsSimpleAdapter = new SimpleAdapter(this, newsData,
				R.layout.item_list_more_news, new String[] { "ID", "Title",
						"Content", "PubTime" }, new int[] { R.id.un_News_id,
						R.id.text_News_Title, R.id.text_News_Content,
						R.id.text_News_PubTime });
		newsListView.setAdapter(newsSimpleAdapter);
	}
	
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case 1:
				updateUI();
				removeProgressBar();
				break;
			case -1:
				getDataError((String) msg.obj);
				break;
			case 5:
				break;
			case 7:
				break;
			case 0:
				break;
			}
		}
	};

	/**
	 * 线程 得到新房列表
	 */
	Runnable getNewHouseNewsListRun = new Runnable() {

		@Override
		public void run() {
			Message msg = handler.obtainMessage();
			String jsonString;
			String url=AppDataConfig.xinfangUrl
					+ AppDataConfig.NewhouseNewsList + "?houseid=" + sHouseId;
			Log.e("url", url);
			jsonString = HttpTools.getJsonContent(url);
			Log.e("JsonString", jsonString);
			newHouseNewsListResult = JsonTools.getNewhouseNewsList(jsonString);
			getData();
			if (newHouseNewsListResult.getSuccess()) {
				msg.arg1 = 1;
			} else {
				msg.arg1 = -1;
				msg.obj = newHouseNewsListResult.getErrorMessage();
			}
			handler.sendMessage(msg);
		}
	};

	// 得到data数据
	private void getData() {
		newsData = new ArrayList<Map<String, Object>>();
		List<Object> newhouseNewsList;
		Map<String, Object> map;
		if (newHouseNewsListResult.getSuccess()) {
			Log.e("size", newHouseNewsListResult.getData().size() + "");
			newhouseNewsList = newHouseNewsListResult.getData();

			for (int i = 0; i < newhouseNewsList.size(); i++) {
				NewhouseNewsDetail news=(NewhouseNewsDetail) newhouseNewsList.get(i);
				map = new HashMap<String, Object>();
				map.put("ID", news.getId());
				String title=news.getTitle();
				Log.e("title", title);
				if(title.length()>16){
					title=title.substring(0, 16);
					title+="…";
				}
				map.put("Title", title);
				map.put("Content", news.getContent().trim());
				map.put("PubTime", news.getPubTime());
				newsData.add(map);
			}

		}

	}
	private void getDataError(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton(getResources().getString

				(R.string.cancel), new DialogInterface.OnClickListener()

				{
					public void onClick

					(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton(getResources().getString

				(R.string.try_again), new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						showProgressBar();
						new Thread(getNewHouseNewsListRun).start();
					}
				}).show();
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_back:
			this.finish();
			break;

		default:
			break;
		}
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		TextView idText=(TextView) arg1.findViewById(R.id.un_News_id);
		TextView pubTime = (TextView) arg1.findViewById(R.id.text_News_PubTime);
		Intent intent=new Intent(this, HouseNewsDetailActivity.class);
		intent.putExtra("houseNewsId", idText.getText()+"");
		intent.putExtra("pubTime", pubTime.getText());
		startActivity(intent);
		
	}

}
